home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / LList Mgr / LList Mgr For Think Pascal / Sample LList.p < prev    next >
Encoding:
Text File  |  1994-05-22  |  7.3 KB  |  279 lines  |  [TEXT/PJMM]

  1. {************************************************************}
  2. {*                                                            *}
  3. {* Sample LList                                                *}
  4. {*                                                            *}
  5. {* The following code demonstrates the use of an LList in     *}
  6. {* a modal dialog. See LList.p for documentation on each    *}
  7. {* LList procedure.                                            *}
  8. {*                                                            *}
  9. {************************************************************}
  10.  
  11. program SampleLList;
  12.  
  13.     uses
  14.         LList;
  15.  
  16.     const
  17.         DIALOG_RSRC_ID = 128;
  18.         DIALOG_LIST_ITEM = 3;
  19.         DIALOG_SORT_FWD_ITEM = 4;
  20.         DIALOG_SORT_BKWD_ITEM = 5;
  21.         DIALOG_OK_ITEM = 1;
  22.         DIALOG_CANCEL_ITEM = 2;
  23.  
  24.     var
  25.         city: Str255;
  26.         state: Str255;
  27.         someNumber: Str255;
  28.  
  29. {************************************************************}
  30.  
  31.     function myDialogFilter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): Boolean;
  32.  
  33.         var
  34.             remPort: GrafPtr;
  35.             thePt: Point;
  36.             theRect: Rect;
  37.             theChar: char;
  38.             doubleClick: integer;
  39.  
  40.     begin
  41.         case theEvent.what of
  42.             keyDown: 
  43.                 begin
  44.     {if key pressed, handle return key}
  45.                     theChar := char(BitAnd(theEvent.message, charCodeMask));
  46.                     if (theChar = char($0D)) or (theChar = char($03)) then
  47.                         begin
  48.                             itemHit := DialogPeek(theDialog)^.aDefItem;
  49.                             myDialogFilter := true;
  50.                             Exit(myDialogFilter);
  51.                         end;
  52.                     myDialogFilter := false;
  53.                     Exit(myDialogFilter);
  54.                 end;
  55.  
  56.             mouseDown: 
  57.                 begin
  58.     {get where the click occured in global coords}
  59.                     thePt := theEvent.where;
  60.  
  61.     {save the current port}
  62.                     GetPort(remPort);
  63.                     SetPort(theDialog);
  64.  
  65.     {convert click to local coords}
  66.                     GlobalToLocal(thePt);
  67.  
  68.     {get a copy of the list's view rectangle}
  69.                     theRect.top := LListPtr(WindowPeek(theDialog)^.refCon)^.view.top;
  70.                     theRect.bottom := LListPtr(WindowPeek(theDialog)^.refCon)^.view.bottom;
  71.                     theRect.left := LListPtr(WindowPeek(theDialog)^.refCon)^.view.left;
  72.                     theRect.right := LListPtr(WindowPeek(theDialog)^.refCon)^.view.right + 16;
  73.  
  74.                     if not PtInRect(thePt, theRect) then
  75.                         begin
  76.     {mouse was not clicked in list}
  77.                             SetPort(remPort);
  78.                             myDialogFilter := false;
  79.                             Exit(myDialogFilter);
  80.                         end;
  81.  
  82.     {mouse was clicked in list}
  83.                     itemHit := DIALOG_LIST_ITEM;
  84.                     doubleClick := LLClick(LListPtr(WindowPeek(theDialog)^.refCon), thePt, theEvent.modifiers);
  85.  
  86.     {automatically push OK if user double-clicks}
  87.                     if (doubleClick = 1) then
  88.                         itemHit := DialogPeek(theDialog)^.aDefItem
  89.                     else
  90.                         itemHit := DIALOG_LIST_ITEM;
  91.  
  92.     {restore port and let ModalDialog know we handled event}
  93.                     SetPort(remPort);
  94.                     myDialogFilter := true;
  95.                     Exit(myDialogFilter);
  96.                 end;
  97.  
  98.             updateEvt: 
  99.                 begin
  100.                     BeginUpdate(theDialog);
  101.                     LLUpdate(LListPtr(WindowPeek(theDialog)^.refCon));
  102.                     DrawDialog(theDialog);
  103.                     EndUpdate(theDialog);
  104.                     myDialogFilter := false;
  105.                     Exit(myDialogFilter);
  106.                 end;
  107.  
  108.             otherwise
  109.                 myDialogFilter := false;
  110.  
  111.         end;
  112.     end; {myDialogFilter}
  113.  
  114.  
  115. {************************************************************}
  116.  
  117.     procedure DoSampleLList (var city: Str255; var state: Str255; var someNumber: Str255);
  118.  
  119.         var
  120.             theDialog: DialogPtr;
  121.             itemHit: integer;
  122.             iType: integer;
  123.             iHandle: Handle;
  124.             iRect: Rect;
  125.             theList: LListPtr;
  126.             row: LRowPtr;
  127.             dataLen: integer;
  128.             text: Str255;
  129.  
  130.     begin
  131. {if the user selects a row and presses OK, city, state,}
  132. {and someNumber will contain data from the selected row}
  133.         city := '';
  134.         state := '';
  135.         someNumber := '';
  136.  
  137. {get the sample dialog}
  138.         theDialog := GetNewDialog(DIALOG_RSRC_ID, nil, WindowPtr(-1));
  139.  
  140. {get Rect of list dialog item}
  141.         GetDItem(theDialog, DIALOG_LIST_ITEM, iType, iHandle, iRect);
  142.  
  143. {create a list in the dialog 16 pixels high with 3 columns}
  144.         theList := LLNew(iRect, theDialog, 16, 3, 1, LLOnlyOne);
  145.  
  146.         theList^.colDesc[1].justify := 1;        {center column 2}
  147.         theList^.colDesc[2].justify := -1;    {right justify column 3}
  148.  
  149. {remember theList for myDialogFilter}
  150.         WindowPeek(theDialog)^.refCon := longint(theList);
  151.  
  152. {turn off list drawing while we add rows of data to the list}
  153.         LLDoDraw(theList, 0, 0);
  154.  
  155. {add some rows with data to the list}
  156.         row := LLAddRow(theList, nil);
  157.         text := 'Georgia';
  158.         LLSetCell(theList, row, 0, length(text), pointer(ord(@text) + 1));
  159.         text := 'Atlanta';
  160.         LLSetCell(theList, row, 1, length(text), pointer(ord(@text) + 1));
  161.         text := '1';
  162.         LLSetCell(theList, row, 2, length(text), pointer(ord(@text) + 1));
  163.  
  164.         row := LLAddRow(theList, nil);
  165.         text := 'Virginia';
  166.         LLSetCell(theList, row, 0, length(text), pointer(ord(@text) + 1));
  167.         text := 'Roanoke';
  168.         LLSetCell(theList, row, 1, length(text), pointer(ord(@text) + 1));
  169.         text := '22';
  170.         LLSetCell(theList, row, 2, length(text), pointer(ord(@text) + 1));
  171.  
  172.         row := LLAddRow(theList, nil);
  173.         text := 'Virginia';
  174.         LLSetCell(theList, row, 0, length(text), pointer(ord(@text) + 1));
  175.         text := 'Norfolk';
  176.         LLSetCell(theList, row, 1, length(text), pointer(ord(@text) + 1));
  177.         text := '333';
  178.         LLSetCell(theList, row, 2, length(text), pointer(ord(@text) + 1));
  179.  
  180.         row := LLAddRow(theList, nil);
  181.         text := 'Indiana';
  182.         LLSetCell(theList, row, 0, length(text), pointer(ord(@text) + 1));
  183.         text := 'Ft. Wayne';
  184.         LLSetCell(theList, row, 1, length(text), pointer(ord(@text) + 1));
  185.         text := '4444';
  186.         LLSetCell(theList, row, 2, length(text), pointer(ord(@text) + 1));
  187.  
  188. {turn list drawing back on}
  189.         LLDoDraw(theList, 1, 0);
  190.  
  191. {initially disable OK button}
  192.         GetDItem(theDialog, DIALOG_OK_ITEM, iType, iHandle, iRect);
  193.         HiliteControl(ControlHandle(iHandle), 255);
  194.  
  195.         repeat
  196.             ModalDialog(@myDialogFilter, itemHit);
  197.  
  198.             case itemHit of
  199.                 DIALOG_SORT_FWD_ITEM: 
  200.                     begin
  201.     {sort the list by State, city, case-insensitive, in-order}
  202.                         LLSort(theList, 0, 1, -1, 1, 1);
  203.                     end;
  204.  
  205.                 DIALOG_SORT_BKWD_ITEM: 
  206.                     begin
  207.     {sort the list by State, City, case-insensitive, reverse-order}
  208.                         LLSort(theList, 0, 1, -1, 1, -1);
  209.                     end;
  210.  
  211.                 otherwise
  212.                     begin
  213.     {dim OK button if no row is selected}
  214.                         row := LLNextRow(theList, nil);
  215.                         if LLGetSelect(theList, row, 1) = 0 then
  216.                             begin
  217.                                 HiliteControl(ControlHandle(iHandle), 255);
  218.                                 if itemHit = DIALOG_OK_ITEM then
  219.                                     itemHit := 0; {in case user hit return}
  220.                             end
  221.                         else
  222.                             HiliteControl(ControlHandle(iHandle), 0);
  223.                     end;
  224.  
  225.             end;
  226.  
  227.         until (itemHit = DIALOG_OK_ITEM) or (itemHit = DIALOG_CANCEL_ITEM);
  228.  
  229. {if user presses ok get the city name from the selected row}
  230.         if itemHit = DIALOG_OK_ITEM then
  231.             begin
  232.     {get pointer to first row}
  233.                 row := LLNextRow(theList, nil);
  234.  
  235.     {find first selected row; start looking in first row}
  236.                 if LLGetSelect(theList, row, 1) = 1 then
  237.                     begin
  238.     {get selected state}
  239.                         dataLen := 255;
  240.                         LLGetCell(theList, row, 0, dataLen, pointer(ord(@state) + 1));
  241.     {$PUSH}
  242.     {$R-}
  243.                         state[0] := char(dataLen);
  244.     {$POP}
  245.  
  246.     {get selected city}
  247.                         dataLen := 255;
  248.                         LLGetCell(theList, row, 1, dataLen, pointer(ord(@city) + 1));
  249.     {$PUSH}
  250.     {$R-}
  251.                         city[0] := char(dataLen);
  252.     {$POP}
  253.  
  254.     {get selected someNumber}
  255.                         dataLen := 255;
  256.                         LLGetCell(theList, row, 2, dataLen, pointer(ord(@someNumber) + 1));
  257.     {$PUSH}
  258.     {$R-}
  259.                         someNumber[0] := char(dataLen);
  260.     {$POP}
  261.                     end;
  262.             end;
  263.  
  264. {dispose of list}
  265.         LLDispose(theList);
  266.  
  267. {dispose of dialog}
  268.         DisposDialog(theDialog);
  269.     end; {DoSampleLList}
  270.  
  271. {************************************************************}
  272.  
  273. begin
  274.     FlushEvents(everyEvent, 0);
  275.     InitCursor;
  276.     DoSampleLList(city, state, someNumber);
  277. end. {SampleLList}
  278.  
  279. {************************************************************}